package horncomb.bpm.persistent;

import java.util.Collection;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.googlecode.horncomb.bean.filter.SimpleMapConditionSet;
import com.googlecode.horncomb.orm.ibatis.SqlMapEntityDaoImpl;

import horncomb.bpm.process.core.Token;

/**
 * DefaultTokenEntity data access object
 * <p>
 * 负责人/principal:<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:<br/>
 * @author
 * @version $Id$
 */
public class DefaultTokenDao extends SqlMapEntityDaoImpl<Token> {
	private static final long serialVersionUID = 6742918447181507874L;

	/**
	 * 根据业务Id获取token
	 * @param bizEntityId
	 *            bizEntityId必须全局唯一
	 * @return
	 */
	public Token queryTokenByBizEntityId(String bizEntityId) {
		List<? extends Token> tokens = this.query(new SimpleMapConditionSet()
				.addConstraint("bizEntityId", bizEntityId), null);
		return tokens.size() > 0 ? tokens.get(0) : null;
	}

	/**
	 * 批量根据业务id获取token
	 * @param bizEntityIds
	 * @return
	 */
	public List<? extends Token> queryTokenByBizEntityIds(
			Collection<String> bizEntityIds) {
		return this.query(new SimpleMapConditionSet().addConstraint(
				"bizEntityIds", bizEntityIds.toArray()), null);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void insert(Token entity) throws DataAccessException {
		this.insert(entity.getEntity(), null);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void update(Token entity) throws DataAccessException {
		this.update(entity.getEntity(), null);
	}

	/**
	 * @param parentId
	 * @return
	 */
	public List<? extends Token> querySubTokens(String parentId) {
		return this.query(new SimpleMapConditionSet().addConstraint("parentId",
				parentId), null);
	}

	/**
	 * @param parentTokenId
	 * @param stateIds
	 * @return
	 */
	public int countSubTokenInState(String parentId, String[] stateIds) {
		return this.queryCount(new SimpleMapConditionSet().addConstraint(
				"parentId", parentId).addConstraint("stateIds", stateIds));
	}

	/**
	 * @param parentId
	 * @return
	 */
	public int countSubTokens(String parentId) {
		return this.queryCount(new SimpleMapConditionSet().addConstraint(
				"parentId", parentId));
	}
}
